package com.dunqi.gpm.cangxi.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dunqi.gpm.cangxi.MainActivity;
import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseFragment;
import com.dunqi.gpm.cangxi.common.base.RecyclerAdapter;
import com.dunqi.gpm.cangxi.common.util.CommonUtil;
import com.dunqi.gpm.cangxi.common.util.LogUtils;
import com.dunqi.gpm.cangxi.common.util.SwipeUtil;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.activity.ApprovalReviewActivity;
import com.dunqi.gpm.cangxi.home.activity.EarlyWarningTipsActivity;
import com.dunqi.gpm.cangxi.home.activity.InChargeProjectActivity;
import com.dunqi.gpm.cangxi.home.activity.InstructionsProjectActivity;
import com.dunqi.gpm.cangxi.home.activity.KnotActivity;
import com.dunqi.gpm.cangxi.home.activity.KnotListActivity;
import com.dunqi.gpm.cangxi.home.activity.MeetingRoomActivity;
import com.dunqi.gpm.cangxi.home.activity.PlanAuditActivity;
import com.dunqi.gpm.cangxi.home.activity.ProjectDayReportActivity;
import com.dunqi.gpm.cangxi.home.activity.ProjectListActivity;
import com.dunqi.gpm.cangxi.home.activity.ProjectProgressActivity;
import com.dunqi.gpm.cangxi.home.activity.ProjectSupervisoryManagementActivity;
import com.dunqi.gpm.cangxi.home.activity.ProjectTrendActivity;
import com.dunqi.gpm.cangxi.home.activity.QuestionSolveActivity;
import com.dunqi.gpm.cangxi.home.activity.ReportAuditActivity;
import com.dunqi.gpm.cangxi.home.activity.SupervisoryManagementActivity;
import com.dunqi.gpm.cangxi.home.activity.WarehouseAuditActivity;
import com.dunqi.gpm.cangxi.home.bean.HomeType;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.mine.util.UserUtil;
import com.dunqi.gpm.cangxi.WeekReport.WeekReportListControlerAcitivty;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

/**
 * Created by 空 on 2017/11/24 0024.
 */

public class HomeFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    ArrayList<HomeType> types = new ArrayList<>();
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout swipeRefresh;
    private RecyclerAdapter<HomeType> adapter;

    public static int homeMenuId = -1;

    String[] menuUrls = {"instruction_query", "progress_report", "daily_report", "knot_evaluation",
            "report_review", "conference_room", "work_instructions", "my_attention", "divided_project",
            "complete_pre_stage", "start_pre_stage", "query_project", "annual_plan_audit", "work_trend",
            "new_msg", "plan_audit", "warning_tips", "dynamic_approval", "problem_coordination", "DBGL", "zbg_zjh", "zbg_zbg", "zbg_zbsh"};

    int[] drawables = {R.mipmap.type0, R.mipmap.type1, R.mipmap.type2, R.mipmap.type3, R.mipmap.type4,
            R.mipmap.type5, R.mipmap.type6, R.mipmap.type7, R.mipmap.type8, R.mipmap.type9, R.mipmap.type10,
            R.mipmap.type11, R.mipmap.type12, R.mipmap.type13, R.mipmap.type13, R.mipmap.type15,
            R.mipmap.type16, R.mipmap.type17, R.mipmap.type18, R.mipmap.dbgl, R.mipmap.type4, R.mipmap.type2, R.mipmap.type9};

//    APP端	批示查询	APP主页	instruction_query	GZPS	6	 	2017-11-27 21:56:41
//    APP端	进度汇报	APP主页	progress_report	JDHB	3	 	2017-11-27 21:52:46
//    APP端	每日汇报	APP主页	daily_report	MRHB	2	 	2017-11-27 21:51:06
//    APP端	结项评价	APP主页	knot_evaluation	JSPJ	4	 	2017-11-27 21:49:31
//    APP端	汇报审核	APP主页	report_review	HBSH	3	 	2017-11-27 21:42:10
//    APP端	会议室	APP主页	conference_room	HYS	6	 	2017-11-27 21:38:18
//    APP端	工作批示	APP主页	work_instructions	GZPS	5	 	2017-11-27 21:36:27
//    APP端	我的关注	APP主页	my_attention	WDGZ	3	 	2017-11-27 21:34:40
//    APP端	分管项目	APP主页	divided_project	FGXM	2	 	2017-11-27 21:32:52
//    APP端	审批审核	APP主页	complete_pre_stage	 	5	 	2017-11-16 01:00:19
//    APP端	审批申请	APP主页	start_pre_stage	SPSQ	4	 	2017-11-16 00:59:46
//    APP端	项目查询	APP主页	query_project	XMCX	1	 	2017-11-16 00:57:58
//    APP端	入库审核	APP主页	annual_plan_audit	RKSH	2	 	2017-11-16 00:57:27
//    APP端	工作动态	APP主页	work_trend	GZDT	4	 	2017-11-16 00:56:19
//    APP端	消息动态	APP主页	new_msg	 	1	 	2017-11-16 00:52:43

    @Override
    protected void refreshData(Bundle savedInstanceState) {
        net();
    }

    private void net() {
        LogUtils.i(UserUtil.getUserId());
        Disposable subscribe = ApiFactory.getInstance()
                .getUserMenu(CommonUtil.getDeviceId(),UserUtil.getUserId())
                .compose(RxSchedulers.<ArrayList<HomeType>>io_main())
                .subscribe(types -> {
                    swipeRefresh.setRefreshing(false);
                    if (types == null) {
                        return;
                    }

                    this.types.clear();
                    for (HomeType type : types) {
                        if ("homepage".equals(type.getMenuUrl())) {
                            types.remove(type);
                            break;
                        }
                    }

                    this.types.addAll(types);
                    adapter.notifyDataSetChanged();
                }, throwable -> swipeRefresh.setRefreshing(false));

        addDisposables(subscribe);
    }

    @Override
    public boolean hasStateView() {
        return true;
    }

    @Override
    protected void initUiAndListener() {
        titleBar.setBackGone();
        initIcon();
        SwipeUtil.init(swipeRefresh);

        recycler.setLayoutManager(new GridLayoutManager(context, 3));
        adapter = new RecyclerAdapter<HomeType>(R.layout.item_home, types) {

            @Override
            protected void convert(BaseViewHolder helper, HomeType item) {
                helper.setText(R.id.des_tv, item.getMenuLabel());

                ImageView image_iv = helper.getView(R.id.image_iv);

                for (int i = 0; i < menuUrls.length; i++) {
                    if (menuUrls[i].equals(item.getMenuUrl())) {
                        Glide.with(getActivity())
                                .load(drawables[i])
                                .into(image_iv);
                        break;
                    }

                }
            }
        };
        recycler.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        swipeRefresh.setOnRefreshListener(this);
    }

    private void initIcon() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        HomeType homeType = types.get(position);
        LogUtils.w("homeType", homeType);
        String menuUrl = homeType.getMenuUrl();

        for (int i = 0; i < this.menuUrls.length; i++) {
            if (this.menuUrls[i].equals(menuUrl)) {
                homeMenuId = i;
                break;
            }
        }
        LogUtils.w("homeType", "homeMenuId:" + homeMenuId);
        Intent intent;
        switch (homeMenuId) {
            case Constance.MainMenu.MENU_INSTRUCTION_QUERY:
                //批示查询
                startActivity(InstructionsProjectActivity.class);
                break;
            case Constance.MainMenu.MENU_PROGRESS_REPORT:
                //进展汇报
                startActivity(ProjectProgressActivity.class);
                break;
            case Constance.MainMenu.MENU_DAILY_REPORT:
                //每日进展汇报
                startActivity(ProjectDayReportActivity.class);
//                DayReportActivity.start(getContext(), null);
                break;
            case Constance.MainMenu.MENU_KNOT_EVALUATION:
                //结项评价
                startActivity(KnotListActivity.class);
                break;
            case Constance.MainMenu.MENU_REPORT_REVIEW:
                startActivity(ReportAuditActivity.class);
                break;
            case Constance.MainMenu.MENU_CONFERENCE_ROOM:
                //会议室
//                String url = "http://183.223.157.225:6713/mag/hls/eefa569a58c94ca3920d0986d1d0bd48/0/live.m3u8";
//                Video2Activity.start(getContext(), url, url,"会议室");
                startActivity(MeetingRoomActivity.class);
                break;
            case Constance.MainMenu.MENU_WORK_INSTRUCTIONS:
                //工作批示
//                startActivity(WorkInstructionsActivity.class);
                startActivity(InstructionsProjectActivity.class);
                break;
            case Constance.MainMenu.MENU_MY_ATTENTION:
                //我的关注
                startActivity(AttentionProjectActivity.class);
                break;
            case Constance.MainMenu.MENU_DIVIDED_PROJECT:
                //分管项目
                startActivity(InChargeProjectActivity.class);
                break;
            case Constance.MainMenu.MENU_COMPLETE_PRE_STAGE:
                //审批审核
                startActivity(ApprovalReviewActivity.class);
                break;
            case Constance.MainMenu.MENU_START_PRE_STAGE:
                //审批申请
                startActivity(KnotActivity.class);
                break;
            case Constance.MainMenu.MENU_QUERY_PROJECT:
                //项目查询
//                startActivity(ProjectInquiryActivity.class);
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("uda", "false");
                ProjectListActivity.start(getActivity(), hashMap);
                break;
            case Constance.MainMenu.MENU_ANNUAL_PLAN_AUDIT:
                //入库审核
                startActivity(WarehouseAuditActivity.class);
                break;
            case Constance.MainMenu.MENU_WORK_TREND:
                //工作动态
                String audit_statuss = "";
                int levelType = UserUtil.getLevelType();
                if (UserUtil.userFromNdrcOrg()) {
                    audit_statuss = "1,2,3";
                }
                if (UserUtil.userIsLeader()) {
                    audit_statuss = "2";
                }
                ProjectTrendActivity.start(context, audit_statuss);
                break;
            case Constance.MainMenu.MENU_NEW_MSG:
                break;
            case Constance.MainMenu.MENU_PLAN_AUDIT:
                //计划审核
                startActivity(PlanAuditActivity.class);
                break;
            case Constance.MainMenu.MENU_WARNING_TIPS:
                //预警提示
                startActivity(EarlyWarningTipsActivity.class);
                break;
            case Constance.MainMenu.MENU_DYNAMIC_APPROVAL:
                //动态审核
//                startActivity(ProjectTrendActivity.class);
                ProjectTrendActivity.start(context, "1");
                break;
            case Constance.MainMenu.MENU_PROBLEM_COORDINATION:
                //问题协调
                startActivity(QuestionSolveActivity.class);
                break;
            case Constance.MainMenu.MENU_SUPERVISORY_MANAGEMENT:
                //督办管理
                if (UserUtil.userFromRespOrg()) {
                    SupervisoryManagementActivity.start(context, null);
                } else {
                    startActivity(ProjectSupervisoryManagementActivity.class);
                }
                break;
            case Constance.MainMenu.MENU_WEEK_PLAN:
                //周计划
                intent = new Intent(context, WeekReportListControlerAcitivty.class);
                intent.putExtra("type", "zbg_zjh");
                intent.putExtra("menus", new Gson().toJson(types));
                startActivity(intent);
                break;
            case Constance.MainMenu.MENU_WEEK_REPORT:
                //周报告
                intent = new Intent(context, WeekReportListControlerAcitivty.class);
                intent.putExtra("type", "zbg_zbg");
                intent.putExtra("menus", new Gson().toJson(types));
                startActivity(intent);
                break;
            case Constance.MainMenu.MENU_WEEK_CHECK:
                //周报审核
                intent = new Intent(context, WeekReportListControlerAcitivty.class);
                intent.putExtra("type", "zbg_zbsh");
                intent.putExtra("menus", new Gson().toJson(types));
                startActivity(intent);
                break;
            default:
                //homepage
                if (getActivity() instanceof MainActivity) {
                    ((MainActivity) getActivity()).select(0);
                }
        }
    }

    private void startActivity(Class<?> cls) {
        startActivity(new Intent(context, cls));
    }

    @Override
    public void onRefresh() {
        net();
    }
}